﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel.Composition;
using ProBugTracker.Module.Login.Interfaces;
using Microsoft.Practices.Prism.ViewModel;
using ProBugTracker.Infrastructure;
using Microsoft.Practices.Prism.Commands;
using ProBugTracker.Module.Login.UserHandlingService;
using Microsoft.Practices.Prism.Regions;

namespace ProBugTracker.Module.Login.ViewModels
{
    [Export(typeof(ILoginViewModel))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class LoginViewModel : NotificationObject, ILoginViewModel, IRegionMemberLifetime, IConfirmNavigationRequest

    {
        private UserHandlingServiceClient userHandlingServiceClient;
        public event Func<bool> ShouldNavigateFromCurrentViewEvent;

        [ImportingConstructor]
        public LoginViewModel()
        {
            userHandlingServiceClient = new UserHandlingServiceClient();
            ShouldNavigateFromCurrentViewEvent += new Func<bool>(LoginViewModel_ShouldNavigateFromCurrentViewEvent);
            //this.LogIn = new DelegateCommand<object>(this.LogInExecute);
        }

        bool LoginViewModel_ShouldNavigateFromCurrentViewEvent()
        {
            return true;
        }

        public ICommand LogIn { get; set; }

        private void LogInExecute(object obj)
        {
            LogInToSystem(UserName, Password);
        }

        private string _userName;
        public string UserName
        {
            get { return _userName; }
            set
            {
                if (_userName != value)
                {
                    _userName = value;
                    this.RaisePropertyChanged("UserName");
                }
            }
        }

        private string _password;
        public string Password 
        {
            get { return _password; }
            set
            {
                if (_password != value)
                {
                    _password = value;
                    this.RaisePropertyChanged("Password");
                }
            }
        }

        private string _message;
        public string Message 
        {
            get { return _message; }
            set
            {
                if (_message != value)
                {
                    _message = value;
                    this.RaisePropertyChanged("Message");
                }
            }
        }
        private bool _isValidUser;
        public bool IsValidUser
        { 
            get {return _isValidUser ;}
            set
            {
                if (_isValidUser != value)
                {
                    _isValidUser = value;
                    this.RaisePropertyChanged("IsValidUser");
                }
            }
        }


        public void LogInToSystem(string username, string password)
        {
            userHandlingServiceClient.LogInToSystemCompleted += new EventHandler<LogInToSystemCompletedEventArgs>(userHandlingServiceClient_LogInToSystemCompleted);
            userHandlingServiceClient.LogInToSystemAsync(username, password);
        }

        void userHandlingServiceClient_LogInToSystemCompleted(object sender, LogInToSystemCompletedEventArgs e)
        {
            try
            {
                User user = e.Result;
                IsValidUser=user.ValidationResult.IsValidUser;
                IsValidUser = true;
                if(user.ValidationResult.IsValidUser)
                {
                    
                }
                else
                {
                    Message="Login Failed."+user.ValidationResult.ExceptionMessage;
                }
                
            }
            catch (Exception ex)
            {
                Message="System Exception has been Occurred.\nError Details :"+ex.Message;
            }
        }

        public bool KeepAlive
        {
            get { return true; }
        }


        public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
        {
            LogInToSystem(UserName, Password);

            bool shouldNavigateFromCurrentViewFlag = false;

            if (ShouldNavigateFromCurrentViewEvent != null && IsValidUser)
                shouldNavigateFromCurrentViewFlag = ShouldNavigateFromCurrentViewEvent();

            continuationCallback(shouldNavigateFromCurrentViewFlag);
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            
        }
    }
}
